//
//  TRShareLogsView.m
//  WeiMiLan
//
//  Created by anan on 14/12/14.
//  Copyright (c) 2014年 Mac. All rights reserved.
//

#import "TRCommentLogView.h"
#import "DLAVAlertView.h"
@implementation TRCommentLogView

- (void)reload
{
    // 1.注册
    UINib *nib1 = [UINib nibWithNibName:@"TRCommentLogTableViewCell" bundle:nil];
    [self.tableView registerNib:nib1 forCellReuseIdentifier:@"TRCommentLogTableViewCell"];
    self.tableView.backgroundColor = [UIColor whiteColor];
    
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(ReplyLogActionData:) name:@"kSendMessage" object:nil];
    
    [self addHeader];
    [self addFooter];
}

- (void)loadData:(int)num
{
    NSDictionary *userInfo = [[QLUserManager shareInstance] getLastUserInfoFromNative];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    [dic setObject:QLIntToNumber(num) forKey:@"PAGE_NUM"];
    [dic ql_setInterge:[userInfo ql_intForKey:@"ID"] forKey:@"userId"];
    
    AFHTTPRequestOperationManager *requestManager = [AFHTTPRequestOperationManager manager];
    requestManager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    NSString *strUrl = [NSString stringWithFormat:@"http://ping.vsmu.cn/app/Interface/Comment?type=%@",self.isPingLun ? @"10006" : @"10001"];
    [requestManager GET:strUrl parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if ([responseObject[@"MESSAGE_INFO"][@"CODE"] isEqualToString:@"0000"]) {
            if (num==1) {
                //第一页
                if ([responseObject[@"COMMENT_RSP"][@"COMMENTS"] isKindOfClass:[NSDictionary class]]) {
                    [self.contents addObject:responseObject[@"COMMENT_RSP"][@"COMMENTS"]];
                }else{
                    self.contents = [NSMutableArray arrayWithArray:responseObject[@"COMMENT_RSP"][@"COMMENTS"]];
                }
            }else{
                [self.contents addObjectsFromArray:responseObject[@"COMMENT_RSP"][@"COMMENTS"]];
            }
            
            NSDictionary *pageInfo = responseObject[@"PAGE_INFO"];
            self.nextPage = [pageInfo ql_intForKey:@"NEXT"];
            self.currentPage = [pageInfo ql_intForKey:@"NUMBER"];
            if (self.nextPage>self.currentPage) {
                refreshFooterView.isend = NO;
            }else{
                refreshFooterView.isend = YES;
            }
            
            [self performSelector:@selector(doneWithView:) withObject:nil afterDelay:2.0f];
            
          
            
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [SVProgressHUD showErrorWithStatus:STR_MSG_NETWORK_ERROR];
        [self performSelector:@selector(doneWithView:) withObject:nil afterDelay:2.0f];
    }];
}

- (void)ReplyLogActionData:(NSNotification *)noti {
    
    [self loadData:1];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    long count = self.contents==nil?0:self.contents.count;
    return count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TRCommentLogTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TRCommentLogTableViewCell"];
    if (cell == nil) {
        cell = [[TRCommentLogTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TRCommentLogTableViewCell"];
    }
    NSDictionary *info = [self.contents objectAtIndex:indexPath.row];
    cell.isPingLunCell = self.isPingLun;
    cell.info = info;
    cell.viewController = self.viewController;
    [cell setNeedsDisplay];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TRCommentLogTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TRCommentLogTableViewCell"];
    if (cell == nil) {
        cell = [[TRCommentLogTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TRCommentLogTableViewCell"];
    }
    NSDictionary *info = [self.contents objectAtIndex:indexPath.row];
    cell.info = info;
    cell.viewController = self.viewController;
    return [cell getHeight];
}

#pragma mark - UITableViewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    NSDictionary *dic = [self.contents objectAtIndex:indexPath.row];
    [QLPlayerManager openProductDetailWithId:[dic ql_stringForKey:@"PRODUCT_ID"] viewController:self.viewController];
}

- (void)addFooter
{
    MJRefreshFooterView *footer = [MJRefreshFooterView footer];
    footer.scrollView = self.tableView;
    refreshFooterView = footer;
    footer.beginRefreshingBlock = ^(MJRefreshBaseView *refreshView) {
        [self loadData:self.nextPage];
    };
}

- (void)loadFirstData
{
    [refreshHeaderView beginRefreshing];
}

- (void)addHeader
{
    MJRefreshHeaderView *header = [MJRefreshHeaderView header];
    header.scrollView = self.tableView;
    refreshHeaderView = header;
    header.beginRefreshingBlock = ^(MJRefreshBaseView *refreshView) {
        [self loadData:1];
    };
    [self performSelector:@selector(loadFirstData) withObject:nil afterDelay:0.2];
}

- (void)doneWithView:(id)sender
{
    // 刷新表格
    [self.tableView reloadData];
    [refreshFooterView endRefreshing];
    [refreshHeaderView endRefreshing];
}

#pragma mark - QLMaskNodataViewOnClickDelegate
-(void)onClick:(id)sender
{
    [QLMaskUtil hideNodataMaskWithView:self.tableView];
    [refreshHeaderView beginRefreshing];
}

@end
